﻿using System;
using System.Collections.Generic;
using System.Resources;
using CTS.Com.Domain.Model;
using CTS.Com.Domain.Validate.Model;

namespace CTS.Com.Domain.Helper
{
    public static class ContextHelper
    {
        public static Guid UserCd { get { return UserContext.UserCd; } }
        public static string UserName { get { return UserContext.UserName; } }
        public static string LocaleCd { get { return UserContext.LocaleCd; } }

        public static UserContext UserContext
        {
            get
            {
                var userContext = new UserContext();
                var ctx = CacheHelper.Cache["CTS.Com.UserContext"];
                if (ctx != null && ctx.GetType() == typeof(UserContext)) {
                    userContext = (UserContext)ctx;
                }

                return userContext;
            }
            internal set { CacheHelper.Cache["CTS.Com.UserContext"] = value; }
        }

        public static Rules ValidateRules
        {
            get
            {
                var rules = new Rules();
                var proRules = CacheHelper.Cache["CTS.Com.ValidateRules"];
                if (proRules != null) {
                    var type = proRules.GetType();
                    if (type == typeof(Rules)) rules = (Rules)proRules;
                }
                return rules;
            }
            internal set { CacheHelper.Cache["CTS.Com.ValidateRules"] = value; }
        }

        public static IList<ResourceManager> MessageResources
        {
            get
            {
                var rms = DataHelper.CreateList<ResourceManager>();
                var proRms = CacheHelper.Cache["CTS.Com.MessageResources"];
                if (proRms != null) {
                    rms = (IList<ResourceManager>)proRms;
                }
                return rms;
            }
            internal set { CacheHelper.Cache["CTS.Com.MessageResources"] = value; }
        }

        public static IList<ResourceManager> NameResources
        {
            get
            {
                var rms = DataHelper.CreateList<ResourceManager>();
                var proRms = CacheHelper.Cache["CTS.Com.NameResources"];
                if (proRms != null) {
                    rms = (IList<ResourceManager>)proRms;
                }
                return rms;
            }
            internal set { CacheHelper.Cache["CTS.Com.NameResources"] = value; }
        }

        public static IList<ResourceManager> FormatResources
        {
            get
            {
                var rms = DataHelper.CreateList<ResourceManager>();
                var proRms = CacheHelper.Cache["CTS.Com.FormatResources"];
                if (proRms != null) {
                    rms = (IList<ResourceManager>)proRms;
                }
                return rms;
            }
            internal set { CacheHelper.Cache["CTS.Com.FormatResources"] = value; }
        }
    }
}